import { Injectable } from '@angular/core';
import { Menu, MenuService } from '@delon/theme';
import { environment } from '../../environments/environment';

@Injectable({ providedIn: 'root' })

export class MenuTreeService {
    iconClass = ['anticon-desktop', 'anticon-switcher', 'anticon-tool', 'anticon-file-protect',
        'anticon-cluster', 'anticon-dashboard', 'anticon-file-exclamation', 'anticon-medicine-box',
        'anticon-book', 'anticon-setting', 'anticon-file-sync', 'anticon-team', 'anticon-appstore'
    ];
    menuJson: Menu[] = [];

    constructor(
        private menuService: MenuService
    ) { }

    init(res) {
        this.menuJson = [{ 'text': '目录导航', 'group': true, 'hideInBreadcrumb': true, 'children': [] }];

        for (const item of Object.keys(res)) {
            const c1 = res[item].children;
            if (c1) {
                this.menuJson[0].children.push({
                    'icon': this.iconClass[item],
                    'text': res[item].text,
                    'children': [],
                    'hide': this.visable(res[item].visabel)
                });
                // 二级
                for (const list of Object.keys(c1)) {
                    const c2 = c1[list].children;
                    const child = this.menuJson[0].children[item].children;
                    if (c2) {
                        child.push({
                            'text': '▸ ' + c1[list].text,
                            'children': [],
                            'hide': this.visable(c1[list].visabel)
                        });
                        // 三级
                        for (const data of Object.keys(c2)) {
                            child[list].children.push({
                                'text': '　　' + c2[data].text,
                                'hide': this.visable(c2[data].visabel)
                            });
                            this.switch(c2[data].place, child[list].children[data], c2[data]);
                        }
                    } else {
                        child.push({
                            'text': '　' + c1[list].text,
                            'hide': this.visable(c1[list].visabel)
                        });
                        this.switch(c1[list].place, child[list], c1[list]);
                    }
                }
            } else {
                this.menuJson[0].children.push({
                    'icon': this.iconClass[item],
                    'text': res[item].text,
                    'hide': this.visable(res[item].visabel)
                });
                this.switch(res[item].place, this.menuJson[0].children[item], res[item]);
            }
        }
        // 追加到左侧菜单中，可在路由复用tabs里显示
        this.menuJson[0].children.push({
            'text': 'debugMenu',
            'hide': true,
            'children': [
                { 'text': '我的工作台', 'link': '/main', 'children': [] },
                { 'text': '操作记录', 'link': '/stream', 'children': [] },
                { 'text': '操作记录', 'link': '/tctj/statistics/line', 'children': [] },
                { 'text': '操作记录', 'link': '/tctj/statistics/org', 'children': [] },
                { 'text': '操作记录', 'link': '/tctj/statistics/primary', 'children': [] }
            ]
        });
        this.menuService.add(this.menuJson);
    }

    visable(data) {
        return data === '1' ? false : true // 1显示 !1不显示
    }

    switch(place, child, item) {
        switch (place) {
            case 'outside':
            case 'newtag':
                if (place === 'newtag') {
                    child.target = '_blank';
                }
                switch (item.link.indexOf('http')) {
                    case -1:
                        child.externalLink = environment.outLink + '/#' + item.link;
                        break;
                    default:
                        child.externalLink = item.link;
                        break;
                }
                break;
            case 'alain':
                child.link = '/' + item.link.replace('/', '');
                break;
            case 'jsp':
                child.link = '/remotepage/' + item.id;
                break;
        }
    }

}